#include "db.h"

#include "in.h"

int test_create_indexs()
{
	struct MemDatabase	*db = NULL ;
	struct MemTable		*table = NULL ;
	struct MemIndex		*index = NULL ;
	
	int			nret = 0 ;
	
	printf( "MDBCreateDatabase ...\n" );
	db = MDBCreateDatabase( "mydb" ) ;
	if( db == NULL )
	{
		printf( "ERROR : MDBCreateDatabase failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateDatabase ok , db[%p]\n" , db );
	}
	
	printf( "MDBCreateTable mytable ...\n" );
	table = MDBCreateTable( db , "mytable" , sizeof(struct UserInfo) ) ;
	if( table == NULL )
	{
		printf( "ERROR : MDBCreateTable mytable failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateTable mytable ok , table[%p]\n" , table );
	}
	
	printf( "MDBCreateIndex myindex ...\n" );
	index = MDBCreateIndex( table , "myindex" , MDB_UNIQUEINDEX , MDBFIELD(struct UserInfo,uid) , MDBFIELD_NONE ) ;
	if( index == NULL )
	{
		printf( "ERROR : MDBCreateIndex myindex failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateIndex myindex ok , index[%p]\n" , index );
	}
	
	printf( "MDBCreateIndex myindex2 ...\n" );
	index = MDBCreateIndex( table , "myindex2" , MDB_UNIQUEINDEX , MDBFIELD(struct UserInfo,user_name) , MDBFIELD(struct UserInfo,email) , MDBFIELD_NONE ) ;
	if( index == NULL )
	{
		printf( "ERROR : MDBCreateIndex myindex2 failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateIndex myindex2 ok , index[%p]\n" , index );
	}
	
	printf( "MDBCreateIndex myindex3 ...\n" );
	index = MDBCreateIndex( table , "myindex3" , MDB_UNIQUEINDEX , MDBFIELD(struct UserInfo,email) , MDBFIELD_NONE ) ;
	if( index == NULL )
	{
		printf( "ERROR : MDBCreateIndex myindex3 failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateIndex myindex3 ok , index[%p]\n" , index );
	}
	
	printf( "MDBDropIndex myindex ...\n" );
	nret = MDBDropIndex( table , "myindex" ) ;
	if( nret )
	{
		printf( "ERROR : MDBDropIndex myindex failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBDropIndex myindex ok\n" );
	}
	
	printf( "MDBDropIndexDirectly myindex3 ...\n" );
	nret = MDBDropIndexDirectly( table , index ) ;
	if( nret )
	{
		printf( "ERROR : MDBDropIndexDirectly myindex3 failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBDropIndexDirectly myindex3 ok\n" );
	}
	
	printf( "MDBDropTableDirectly ...\n" );
	nret = MDBDropTableDirectly( db , table ) ;
	if( nret )
	{
		printf( "ERROR : MDBDropTableDirectly mytable failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBDropTableDirectly mytable ok\n" );
	}
	
	printf( "MDBDropDatabase ...\n" );
	MDBDropDatabase( db );
	printf( "MDBDropDatabase ok\n" );
	
	return 0;
}

int main()
{
	return -test_create_indexs();
}

